package com.qyhl.module_activities.act.live;


import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import androidx.core.content.ContextCompat;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.qyhl.module_activities.R;
import com.qyhl.module_activities.R2;
import com.qyhl.module_activities.utils.RecycleViewDivider;
import com.qyhl.webtv.basiclib.base.BaseFragment;
import com.qyhl.webtv.commonlib.entity.act.TeleVoteBean;
import com.qyhl.webtv.commonlib.utils.eventbus.BusFactory;
import com.qyhl.webtv.commonlib.utils.eventbus.Event;
import com.scwang.smartrefresh.header.MaterialHeader;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.footer.ClassicsFooter;
import com.scwang.smartrefresh.layout.listener.OnRefreshListener;
import com.weavey.loading.lib.LoadingLayout;

import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import java.util.ArrayList;

import butterknife.BindView;

public class TeleVoteFragment extends BaseFragment implements TeleVoteContract.TeleVoteView {
    @BindView(R2.id.refresh)
    SmartRefreshLayout refresh;
    @BindView(R2.id.recyclerView)
    RecyclerView recyclerView;
    @BindView(R2.id.layout)
    LoadingLayout layout;

    private TeleVoteContract.TeleVotePresenter presenter;

    private TeleVoteAdapter adapter;
    private ArrayList<TeleVoteBean.EntityListBean> list = new ArrayList<>();;


    public static TeleVoteFragment newInstance(int actId, int linkId) {
        TeleVoteFragment fragment = new TeleVoteFragment();
        Bundle bundle = new Bundle();
        bundle.putString("actId", String.valueOf(actId));
        bundle.putString("linkId", String.valueOf(linkId));
        fragment.setArguments(bundle);
        return fragment;
    }

    @Override
    protected View createView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        return inflater.inflate(R.layout.act_fragment_tele_vote, container, false);
    }

    @Override
    protected void create() {
        BusFactory.getBus().register(this);
        presenter = new TeleVotePresenter(this);

        layout.setStatus(LoadingLayout.Loading);

        recyclerView.setLayoutManager(new LinearLayoutManager(getContext()));
        adapter = new TeleVoteAdapter(getActivity(), list, getArguments().getString("actId"), getArguments().getString("linkId"));
        recyclerView.addItemDecoration(new RecycleViewDivider(getActivity(), LinearLayoutManager.VERTICAL,
                20, ContextCompat.getColor(getActivity(), R.color.global_background)));
        recyclerView.setAdapter(adapter);

        refresh.setEnableLoadMore(false);
        refresh.setDisableContentWhenRefresh(true);
        refresh.setRefreshHeader(new MaterialHeader(getContext()));
        refresh.setRefreshFooter(new ClassicsFooter(getContext()));

        refresh.setOnRefreshListener(new OnRefreshListener() {
            @Override
            public void onRefresh(RefreshLayout refreshlayout) {
                presenter.getData(getActivity(), getArguments().getString("actId"), getArguments().getString("linkId"));
            }
        });

        //内容显示
        layout.setOnReloadListener(new LoadingLayout.OnReloadListener() {
            @Override
            public void onReload(View v) {
                layout.setStatus(LoadingLayout.Loading);
                presenter.getData(getActivity(), getArguments().getString("actId"), getArguments().getString("linkId"));
            }
        });

        presenter.getData(getActivity(), getArguments().getString("actId"), getArguments().getString("linkId"));
    }

    @Override
    protected void setListener() {

    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void getMessage(Event.LoginMessage event) {
        if (event != null) {
            if (event.isLogin()) {
                //登录
                layout.setStatus(LoadingLayout.Loading);
                presenter.getData(getActivity(), getArguments().getString("actId"), getArguments().getString("linkId"));
            }
        }
    }

    @Override
    protected void onFirstVisible() {

    }

    @Override
    protected void onVisible() {

    }

    @Override
    protected void onHide() {

    }

    @Override
    public void setError(String msg) {
        refresh.finishRefresh();
        layout.setErrorText(msg);
        layout.setStatus(LoadingLayout.Error);
    }

    @Override
    public void setDetail(TeleVoteBean data) {
        refresh.finishRefresh();
        list.clear();
        list.addAll(data.getEntityList());
        adapter.notifyDataSetChanged();
        layout.setStatus(LoadingLayout.Success);
    }

    @Override
    public void onDestroy() {
        BusFactory.getBus().unregister(this);
        presenter.onDestroy();
        super.onDestroy();
    }
}
